package FileIO;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class BinaryFileUtils {

    //复制输入流的内容到输出流
    public static void copy(InputStream input, OutputStream output) throws IOException {
        byte[] buf = new byte[4096];
        int bytesRead = 0;
        while ((bytesRead = input.read(buf)) != -1) {
            output.write(buf, 0, bytesRead);
        }
    }

    //复制Reader到Writer
    public static void copy(final Reader input, final Writer output) throws IOException {
        char[] cbuf = new char[4096];
        int charsRead = 0;
        while ((charsRead = input.read(cbuf)) != -1) {
            output.write(cbuf);
        }
    }

    //将文件全部内容读到一个字符串中
    public static String readFileToString(final String fileName, final String encoding) throws IOException {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), encoding));
            StringWriter writer = new StringWriter();
            copy(reader, writer);
            return writer.toString();
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

    //将字符串写到文件，参数为文件名、字符串内容和编码类型
    public static void writeStringToFile(final String fileName, final String data, final String encoding) throws IOException {
        Writer writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream(fileName), encoding);
            writer.write(data);
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    //按行将多行数据写到文件，参数为文件名、编码类型、行的集合
    public static void writeLines(final String fileName, final String encoding, final Collection<?> lines) throws FileNotFoundException, UnsupportedEncodingException {
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(fileName, encoding);
            for (Object line : lines) {
                writer.print(line);
            }
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    //按行将文件内容读到一个列表中，参数为文件名、编码类型
    public static List<String> readLines(final String fileName, final String encoding) throws IOException {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), encoding));
            List<String> list = new ArrayList<>();
            String line = reader.readLine();
            while (line != null) {
                list.add(line);
                line = reader.readLine();
            }
            return list;
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }

    //将文件读入字节数组
    public static byte[] readFileToByteArray(String fileName) throws IOException {
        InputStream input = new FileInputStream(fileName);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            copy(input, output);
            return output.toByteArray();
        } finally {
            input.close();
        }
    }
}